﻿@model Sealight.Core.DataAdapter.product
@{
    ViewBag.Title = "Category for Product";
}
<script language="javascript" type="text/javascript">
    function handlerDeleteCategory() {
        $('.delete-category').unbind('click').click(function () {
            var id = $(this).attr('rel');

            var tr = $(this).parent('td').parent('tr');

            JsUtils.ShowLoader('#listCategoryForProduct', 'Xóa...');
            $.ajax({
                type: "POST",
                url: "/admin/product/_DeleteCategoryMapToProduct",
                data: ({id:id}),
                dataType: "json",
                error: function (xhr, status, error) { },
                success: function (d) {
                    if (d != null) {
                        if (d.OperationComplete != null && d.OperationComplete == false && d.ErrorCode != null) {
                            if (d.ErrorFriendlyMessage != null) {
                                ShowErrorMessage(d.ErrorFriendlyMessage);
                            }
                            return;
                        }

                        tr.remove();
                    }
                },
                complete: function (d) {
                    JsUtils.hideLoader('#listCategoryForProduct');
                },
                cache: false
            }); //end .ajax call


        });
    }
    $(function () {
        var data = [];

        handlerDeleteCategory();
        $('#btnSaveMapCategory').click(function () {
            data = [];
            var productId = $('#product_id').val();
            if ($('#list_category_field option:selected').length > 0) {
                $('#list_category_field option:selected').each(function () {
                    if ($(this).val() != '') {
                        var catId = $(this).val();
                        if ($('a', $('#listCategoryForProduct tr td')).filter(function (index) {
                            return $(this).attr('rel') == catId;
                        }).length == 0) {


                            var categoryMap = {
                                category: {
                                    id: $(this).val(),
                                    name: $(this).text()
                                }
                            };
                            data.push(categoryMap);

                        }
                    }
                });
            }

            if (data == null || data.length == 0) return;

            JsUtils.ShowLoader('#listCategoryForProduct', 'Lưu...');

            $.ajax({
                type: "POST",
                url: "/admin/product/_SaveCategoryMapToProduct?id=" + productId,
                data: JSON.stringify(data),
                traditional: true,
                dataType: "json",
                contentType: 'application/json',
                error: function (xhr, status, error) { },
                success: function (d) {
                    if (d != null) {
                        if (d.OperationComplete != null && d.OperationComplete == false && d.ErrorCode != null) {
                            if (d.ErrorFriendlyMessage != null) {
                                ShowErrorMessage(d.ErrorFriendlyMessage);
                            }
                            return;
                        }

                        ShowErrorMessage("Lưu thành công", "thông báo");
                       
                        if (d && d.length) {
                            var tableData = $('#listCategoryForProduct tbody');

                            tableData.children().remove();

                            for (var i = 0; i < d.length; i++) {
                                var tr = '<tr class="rowstyle"><td style="width: 60%;">' + d[i].category.name + '</td><td align="center"> <a rel="' + d[i].id + '" class="delete-category" style="cursor:pointer;"> Xóa </a> </td></tr>'
                                tableData.append(tr);

                            }
                        }

                        handlerDeleteCategory();
                    }
                },
                complete: function (d) {
                    JsUtils.hideLoader('#listCategoryForProduct');
                },
                cache: false
            }); //end .ajax call

        });

    });
</script>
<div>
    <table style="margin-top: 10px" class="table-form">
        <tr>
            <td valign="top" style="padding-left: 5px;">
                Chọn Loại:<br />
                @Html.Partial("_ListCategoryForMapToProduct")
            </td>
        </tr>
    </table>
</div>
<div class="options">
    <input type="button" value="Thêm" id="btnSaveMapCategory" class="adminButtonBlue"
        title="Lưu" />
</div>


<table class="tablestyle" cellspacing="0" rules="all" border="1" id="listCategoryForProduct"
    style="width: 100%; border-collapse: collapse;">
    <thead class="headerstyle">
            <th scope="col" align="center">
                Tên loại
            </th>
            <th align="center" scope="col">
                Xóa
            </th>
        </thead>
    <tbody>
        
        @if (Model != null && Model.category_maps!= null)
        {
            foreach (var cat in Model.category_maps)
            {
                if (cat.category != null)
                {
             <tr class="rowstyle"><td style="width: 60%;">@cat.category.name</td>
             <td align="center"> <a rel="@cat.id" class="delete-category" style="cursor:pointer;"> Xóa </a> </td>
             </tr>
                }
            }
        }
    </tbody>
</table>
